import bb.cascades 1.0

Container {
    signal refreshTriggered
    id: refreshContainer
    property bool readyForRefresh: false
    property bool refreshing: false       // True if currently refreshing the list
    property string refreshedAt: ""
    property int refresh_threshold: 20    // How the user needs to pull to trigger before release to refresh 
    horizontalAlignment: HorizontalAlignment.Fill
    layout: DockLayout {
    }
    
    //TODO: add last refreshed item
    Container {
        id: refreshStatusContainer
        horizontalAlignment: HorizontalAlignment.Center
        verticalAlignment: VerticalAlignment.Center
        
        layout: StackLayout {
            orientation: LayoutOrientation.LeftToRight
        }
        
        ImageView {
            id: refreshImage
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
            preferredHeight: 50.0
            preferredWidth: 50.0
        }
        
        Container {
            topPadding: 20.0
            bottomPadding: 20.0
            
            Label {
                id: refreshStatus
                text: qsTr("下拉刷新")
                textStyle {
                    color: Color.create ("#777777")
                    fontSize: FontSize.PointValue
                    fontSizeValue: 7
                }
                verticalAlignment: VerticalAlignment.Center
                textStyle.textAlign: TextAlign.Center
                horizontalAlignment: HorizontalAlignment.Fill
            }
        }
    
    }
    Container {
        horizontalAlignment: HorizontalAlignment.Fill
        ActivityIndicator {
            id: loadingIndicator
            preferredWidth: 50
            preferredHeight: 50
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
        }
    }
    Divider {
        opacity: 0.0
    }
    attachedObjects: [
        LayoutUpdateHandler {
            id: refreshHandler
            onLayoutFrameChanged: {
                if (refreshing) {
                    return;
                }
                
                //                console.log(qsTr("height:%1,width:%2,x:%3,y:%4").arg(layoutFrame.height).arg(layoutFrame.width).arg(layoutFrame.x).arg(layoutFrame.y));
                
                readyForRefresh = false;
                
                if (layoutFrame.y >= 0) {
                    refreshImage.rotationZ = layoutFrame.y*2;
                    if (layoutFrame.y >= refresh_threshold) {
                        if (! refreshing) {
                            readyForRefresh = true;
                        }
                        refreshStatus.text = qsTr("释放刷新")
                    }
                } else if (layoutFrame.y >= -100) {
                    refreshStatusContainer.visible = true;
                    if (refreshContainer.refreshedAt == "") {
                        refreshStatus.text = qsTr("下拉刷新")
                    } else {
                        refreshStatus.text = qsTr("下拉刷新. 最后更新于") + timeSince(refreshContainer.refreshedAt);
                    }
                    refreshImage.rotationZ = 0;
                } else {
                    //don't refresh
                    refreshImage.rotationZ = 0;
                }
            }
        }
    ]
    function released() {
        if (readyForRefresh) {
            readyForRefresh = false;
            refreshing = false;
            refreshTriggered();
        }
    }
    onRefreshingChanged: {
        if (refreshing) {
            var tmpDate = new Date();
            refreshContainer.refreshedAt = tmpDate.getTime() / 1000;
            
            refreshStatusContainer.visible = false;
            loadingIndicator.visible = true;
            loadingIndicator.running = true;
        } else {
            loadingIndicator.running = false;
            loadingIndicator.visible = false;
            refreshStatusContainer.visible = true;
            refreshContainer.setPreferredHeight(0);
        }
    }
    function timeSince(date) {
        var seconds = Math.floor(((new Date().getTime() / 1000) - date)), interval = Math.floor(seconds / 31536000);
        if (interval > 1) return qsTr("%L1y ago").arg(interval);
        interval = Math.floor(seconds / 2592000);
        if (interval > 1) return qsTr("%L1m ago").arg(interval)
        interval = Math.floor(seconds / 86400);
        if (interval >= 1) return qsTr("%L1d ago").arg(interval)
        interval = Math.floor(seconds / 3600);
        if (interval >= 1) return qsTr("%L1h ago").arg(interval)
        interval = Math.floor(seconds / 60);
        if (interval > 1) return qsTr("%L1m ago").arg(interval)
        return qsTr("刚刚");
    }
    function onListViewTouch(event) {
        refreshContainer.resetPreferredHeight();
        if (event.touchType == TouchType.Up||event.touchType == TouchType.Cancel) { //pulled and released
            released();
        }
    }
}
